<?php

class FunctionsController extends GeanyController {
    
    // <editor-fold defaultstate="collapsed" desc="STATIC & PRIVATE VARIABLE DECLARATION">
    private $validator;
    const _FUNCTION_ID = 'functions';
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CONSTRUCTOR">
  
    public function __construct(){
        parent::__construct();
        $this->loadDefaultValue();
        $this->loadServices();
        $this->validator = null;
    }
	
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ACTION-HANDLER">
  
    public function index(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $data['Model'] = $this->mService->getList();
        
        return View::make('functions/index', $data);
    }

    public function create(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        $functions = new Functions();
        
        try {
            if(Input::get('submit')==null) {
                return $this->createInputView($functions);
            } else {
                $functions = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($functions);
                $newId = $this->mService->insertObject($functions);
                if($newId!==null){
                    return Redirect::to('functions/detail/'.$newId);
                }
                return $this->createInputView($functions);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($functions);
        }
    }
    
    public function detail($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        try {
            $result = $this->mService->getObject($id);
            if($result==null) {
                PageData::addModelError("Function with Id [ ".$id." ] Not Found ");
                Return Redirect::to('functions')->with('ModelState',PageData::getModelState());
            }
            $data['Model'] = $result;
            return View::make('functions/detail', $data);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return Redirect::to('functions');
        }
    }

    public function edit($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $functions = new Functions();
        try {
            if(Input::get('submit')==null){
                $functions = $this->mService->getObject($id);
                return $this->createInputView($functions);
            } else {
                $functions = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($functions);
                if($this->mService->updateObject($functions,$functions->getId())==true){
                    Return Redirect::to('functions/detail/'.$functions->getId());
                }
                return $this->createInputView($functions);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($functions);
        }
    }
    
    public function delete($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        try {
            $this->mService->deleteObject($id);
            Return Redirect::to('functions');
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('functions');
        }
    }
    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="PRIVATE-FUNCTION">
    
    private function loadComboIsEnabled($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsEnabledCombo'] = $arr_data;
    }
    
    private function loadComboIsShow($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsShowCombo'] = $arr_data;
    }
    
    private function loadComboModule($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $moduleService = new ModuleService();
        $modules = $moduleService->getList();
        foreach ($modules as $item){
            $arr_data[$item->getId()] = $item->getName();
        }
        
        $this->data['ModuleCombo'] = $arr_data;
    }
    
    private function createInputView($functions=null){
        $this->loadComboIsEnabled();
        $this->loadComboIsShow();
        $this->loadComboModule();
        $this->data['Model'] = $functions;
        return View::make('functions/input', $this->data);
    }
    
    private function bindData(){
        $data = Input::all();
        
        $functions = New Functions();
        $functions->setId(is_null($data["id"]) ? strtolower($data["name"]) : $data["id"]);
        $functions->setName($data["name"]);
        $functions->setUrl($data["url"]);
        $module = new Module();
        $module->setId($data["module_id"]);
        $module->setIsLoaded(false);
        $functions->setModule($module);
        $functions->setIcon($data["icon"]);
        $functions->setSequence($data["sequence"]);
        $functions->setIsShow($data["is_show"]);
        $functions->setIsEnabled($data["is_enabled"]);

        return $functions;
    }
 
    private function validateInput($data){

        if($data==null) return false;
        $rules = array(
            'name' => 'alpha|required',
            'sequence' => 'numeric|required',
            'url' => 'alpha|required',
            'icon' => '',
            'is_show' => 'boolean',
            'is_enabled' => 'boolean',
        );
        
        $this->validator = Validator::make($data, $rules);
        if($this->validator->fails()) PageData::assignValidatorToModelState ($this->validator);
        return $this->validator->passes();
    }
    
    private function loadDefaultValue(){
        $this->setFunctionId(self::_FUNCTION_ID);
        $this->page_data['_MODULE_NAME'] = $this->getFunctionName();
    }

    private function loadServices(){
        $this->mService = new Functionsservice();
    }
    
    // </editor-fold>
    
}
?>